package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.mapper.JHZF.JhzfMapper;
import com.credithc.customers.dataservice.po.SystemUserPo;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.service.OldJhzfDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;
@Service
@Slf4j
public class OldJhzfDataServiceImpl implements OldJhzfDataService {
    @Autowired
    private JhzfMapper jhzfMapper;
    @Autowired
    SyncDataService syncDataService;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;
    @Override
    public void oldAccount() {
        boolean f =true;
        Long num = jhzfMapper.queryUserMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e =oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    List<SystemUserPo> systemUserPos = jhzfMapper.queryHyrUser(Long.toString(vernier.get()));
                    if (systemUserPos.size() > 0) {
                        bulidJhzfUser(systemUserPos);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidJhzfUser(List<SystemUserPo> systemUserPos) {
        List<CustomerAccountInfoRo> customerAccountInfoRos = new ArrayList<>();
        for (SystemUserPo u:systemUserPos) {
            CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
            customerAccountInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerAccountInfoRo.setSubSystemSign("JHZF");
            customerAccountInfoRo.setSystemSign("JHZF");
            customerAccountInfoRo.setAccountCreateTime(u.getCreatTime());

            customerAccountInfoRo.setAccountNo(u.getCustomerId());
            customerAccountInfoRo.setCustomerNo(u.getCustomerNo());
            customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_BANK_CARD);



            customerAccountInfoRo.setUserSourceId(u.getUserId());
            customerAccountInfoRo.setAccountStatus(StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())?CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
            customerAccountInfoRos.add(customerAccountInfoRo);

        }
        syncDataService.syncCustomerAccount(customerAccountInfoRos);
    }
}
